﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Data.Model;

namespace DataAccess.BikeManage
{
    /// <summary>
    /// 调度管理类
    /// </summary>
    public class Dispatch : DbBase, IDispatch
    {
        /// <summary>
        /// 添加一条调度记录
        /// </summary>
        /// <param name="d">调度详细</param>
        /// <returns></returns>
        public int AddDispatch(schedule d)
        {
            int result = 0;
            if (d != null)
            {
                string sql =
                    string.Format(
                        "insert into schedule([fromaddress],[toaddress],[fromnum],[tonum],[count],[name],[dotime],[status],[memo]) values('{0}','{1}',{2},{3},{4},'{5}',getdate(),{6},'{7}') select @@identity id", d.fromaddress, d.toaddress, d.fromnum, d.tonum, d.count, d.name, d.status, d.memo);

                DataSet dataSet = ExcuteSqlDataSet(sql);
                if (dataSet != null && dataSet.Tables[0].Rows.Count == 1)
                {
                    result = Convert.ToInt32(dataSet.Tables[0].Rows[0]["id"]);
                }
                //CloseConn();
            }
            return result;
        }

        /// <summary>
        /// 调度控制箱的车辆
        /// </summary>
        /// <param name="plusid">增加车辆的控制箱id</param>
        /// <param name="subid">减少车辆的控制箱id</param>
        /// <param name="bikecount">调度车辆的数目</param>
        /// <returns></returns>
        public int DoDispatch(int plusid, int subid, int bikecount)
        {
            int result = 0;
            try
            {
                using (SqlCommand cmd = GetProCommand())
                {
                    cmd.CommandText = "sp_DispatchControlBox";
                    cmd.Parameters.Add("@plusid", DbType.Int32).Value = plusid;
                    cmd.Parameters.Add("@subid", DbType.Int32).Value = subid;
                    cmd.Parameters.Add("@bikecount", DbType.Int32).Value = bikecount;

                    result = cmd.ExecuteNonQuery();
                }
            }
            catch (Exception)
            {

                result = 0;
            }

            return result;

        }

        /// <summary>
        /// 将车辆调入控制箱
        /// </summary>
        /// <param name="bikeids">车辆的id</param>
        /// <param name="controlBoxId">控制箱的id</param>
        /// <returns></returns>
        public bool DispatchBikeToControlBox(List<int> bikeids, int controlBoxId)
        {
            bool result = false;
            string sql = string.Empty;
            int dispatchNum = 0;
            StringBuilder sb = new StringBuilder();
            string bids = string.Empty;
            foreach (int bikeid in bikeids)
            {
                if (bikeid > 0)
                {
                    sb.Append(string.Format("{0},", bikeid));
                }
            }
            if (sb.Length > 0)
            {
                bids = sb.ToString().Substring(0, sb.Length - 1);
            }
            if (!string.IsNullOrEmpty(bids))
            {
                sql = string.Format("UPDATE bike SET [controlboxid]={0} where [bikeid] in ({1})", controlBoxId, bids);

                ExcuteSql(sql);
                //CloseConn();
            }
            return result;
        }

        /// <summary>
        /// 更新车辆停靠位置，就是更新车辆表，将之前的停车柱id修改为新的停车柱id
        /// </summary>
        /// <param name="bikeids">车辆的id</param>
        /// <param name="lockPileId">停车柱的id</param>
        /// <returns></returns>
        public bool DispatchBikeToLockPile(List<int> bikeids, int lockPileId)
        {
            bool result = false;
            string sql = string.Empty;
            int dispatchNum = 0;
            StringBuilder sb = new StringBuilder();
            string bids = string.Empty;
            foreach (int bikeid in bikeids)
            {
                if (bikeid > 0)
                {
                    sb.Append(string.Format("{0},", bikeid));
                }
            }
            if (sb.Length > 0)
            {
                bids = sb.ToString().Substring(0, sb.Length - 1);
            }
            if (!string.IsNullOrEmpty(bids))
            {
                sql = string.Format("UPDATE bike SET [lockpileid]={0} where [bikeid] in ({1})", lockPileId, bids);

                ExcuteSql(sql);
                //CloseConn();
            }
            return result;
        }

    }
}
